package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemMapper;
import com.itheima.service.CheckItemService;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemMapper checkItemMapper;

    //新增
    @Override
    public Result add(CheckItem checkItem) {
        try {
            checkItemMapper.insert(checkItem);
          return   new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
           return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //删除
    @Override
    public Result delete(Integer id) {
        checkItemMapper.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //分页条件查询
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<CheckItem> page =new Page(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List list;
        if(queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0) {
            list = checkItemMapper.selectPage(page,
                    new EntityWrapper<CheckItem>()
                            .eq("code", queryPageBean.getQueryString())
                            .or()
                            .eq("name", queryPageBean.getQueryString())
            );
        }else {
             list = checkItemMapper.selectPage(page, null);
        }
        return new PageResult(page.getTotal(),list);
    }

    //修改
    @Override
    public Result update(CheckItem checkItem) {
        checkItemMapper.updateById(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    //通过id查询
    @Override
    public Result findById(Integer id) {
        CheckItem checkItem = checkItemMapper.selectById(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    //查询所有
    @Override
    public Result findAll() {
        List<CheckItem> list = checkItemMapper.selectList(null);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }
}
